﻿// Name: ImageClass.cs
// Description: Processes all image file content.
// Author: Henry Sheldon http://codewordhank.com
// Original Start Date: 1/20/2010
// 


using System;
using System.Net;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace ClipboardLivePlugin
{
    public class ImageClass
    {

        // Returns a temporary Folder with a fake jpg filename
        public string GetTempJpegDirectory()
        {
            string path = Path.GetFileNameWithoutExtension(Path.GetRandomFileName());
            path += ".jpg";
            string tempPath = Path.GetTempPath();
            Directory.CreateDirectory(tempPath);
            path = Path.Combine(tempPath, path);
            return path;
        }

        // Retrieves an Image from the Internet
        public Image GetImageFromNet(string url)
        {
            if (!url.ToLower().Contains("http://"))
                return null;

            Image webImage = null;

            try
            {
                WebRequest requestPic = WebRequest.Create(url);
                WebResponse responsePic = requestPic.GetResponse();
                webImage = Image.FromStream(responsePic.GetResponseStream());
            }
            catch
            {
                return null;
            }

            return webImage;
        }

        // Converts the bitmap to Jpg using Quality and saves it to the Temporary folder
        public void saveBitmapAsJpeg(string path, Bitmap img, long quality)
        {
            EncoderParameter qualityParam =
               new EncoderParameter(Encoder.Quality, (long)quality);

            ImageCodecInfo jpegCodec =
               this.getEncoderInfo("image/jpeg");

            if (jpegCodec == null)
                return;

            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = qualityParam;

            img.Save(path, jpegCodec, encoderParams);
        }

        // Converts an Image to Jpg and saves it to the Temporary folder
        public void saveImageAsJpeg(string path, Image img, long quality)
        {
            EncoderParameter qualityParam =
               new EncoderParameter(Encoder.Quality, (long)quality);

            ImageCodecInfo jpegCodec =
               this.getEncoderInfo("image/jpeg");

            if (jpegCodec == null)
                return;

            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = qualityParam;

            img.Save(path, jpegCodec, encoderParams);
        }

        // Finds a mimeType in the ImageCodecInfo
        private ImageCodecInfo getEncoderInfo(string mimeType)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

            for (int i = 0; i < codecs.Length; i++)
                if (codecs[i].MimeType == mimeType)
                    return codecs[i];
            return null;
        }

    }
}
